﻿/* Copyright (c) 2011-2012, Zetatron Consulting
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of Zetatron Consulting nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AionCxxLibrary.Packets;
using WorldServer.Models;
using AionCxxLibrary.Enums;
using WorldServer.Services;

namespace WorldServer.Packets.Server
{
    public class ItemPacket
    {
        public static void WriteGlobalInfo(AionServerPacket pkt, Item item)
        {
            pkt.WriteInt(item.GetGUID());
            pkt.WriteInt(item.TemplateID);
            pkt.WriteShort(0x24);
            pkt.WriteInt(item.Template.NameID);
            pkt.WriteShort(0);
        }

        public static void WriteMailItem(AionServerPacket pkt, Item item)
        {
            pkt.WriteInt(item.GetGUID());
            pkt.WriteInt(item.TemplateID);
            pkt.WriteInt(1);
            pkt.WriteInt(0);
            pkt.WriteShort(0x24);
            pkt.WriteInt(item.Template.NameID);
            pkt.WriteShort(0);
        }

        public static void WriteCommonItem(AionServerPacket pkt, Item item)
        {
            short len = 32;
            len += (short)(item.Crafter.Length * 2);
            pkt.WriteShort(len);
            pkt.WriteByte(0);
            pkt.WriteUnsignedShort(item.Template.Mask);
            pkt.WriteLong(item.Count);
            pkt.WriteString(item.Crafter);
            pkt.WriteByte(0);
            pkt.WriteLong(item.Template.ExpireTime); // TemporaryItemTime
            pkt.WriteInt(item.TemporaryTradeTimeLeft);
            pkt.WriteInt(0);
            pkt.WriteShort(0);
        }

        public static void WriteStigmaStone(AionServerPacket pkt, Item stigma)
        {
            pkt.WriteShort(0x159);
            pkt.WriteByte(6);
            pkt.WriteInt(stigma.Slot);
            pkt.WriteByte(7);
            pkt.WriteShort(0); // Stigma Skill ID
            pkt.WriteInt(0);
            pkt.WriteShort(0);
            pkt.WriteInt(0); // Requires Shards to equip
            pkt.WriteBytes(new byte[160]);
            pkt.WriteInt(1);
            pkt.WriteBytes(new byte[82]);
            pkt.WriteShort(0x0B);
            pkt.WriteByte(0);
            pkt.WriteInt(stigma.TemplateID);
            pkt.WriteBytes(new byte[43]);
            pkt.WriteInt(66110);
            pkt.WriteBytes(new byte[27]);
            pkt.WriteUnsignedShort((ushort)stigma.Slot);
        }

        public static void WriteKinah(AionServerPacket pkt, Item kinah)
        {
            pkt.WriteShort(0x20);
            pkt.WriteByte(0);
            pkt.WriteUnsignedShort(kinah.Template.Mask);
            pkt.WriteLong(kinah.Count);
            pkt.WriteBytes(new byte[21]);
            pkt.WriteShort(0xFF);
        }

        public static void WriteWeapon(AionServerPacket pkt, Item weapon)
        {
            int placeHolder, sizeLoc, size = 0;
            pkt.WriteShort(0x67);
            sizeLoc = (int)pkt.Position;

            if (weapon.FusionedItem != null)
            {
                pkt.WriteByte(0x0E);
                pkt.WriteInt(weapon.FusionedItem.TemplateID);
                // write fusion stones
                pkt.WriteBytes(new byte[24]);
                pkt.WriteByte((byte)(weapon.OptionalSocketFlag == Item.OptionalSocket_All || weapon.OptionalSocketFlag == Item.OptionalSocket_FusionedItem ? 1 : 0));
            }

            pkt.WriteByte(6);
            pkt.WriteInt(weapon.Equipped ? weapon.Slot : 0);
            pkt.WriteByte(1);

            pkt.WriteInt((int)weapon.Template.EquipmentSlot);
            pkt.WriteInt(weapon.FusionedItem != null ? 0 : 2);
            
            pkt.WriteByte(0x0B);
            pkt.WriteByte((byte)(weapon.SoulBound ? 1 : 0));
            pkt.WriteByte(weapon.EnchantLevel);
            pkt.WriteInt(weapon.SkinID == 0 ? weapon.TemplateID : weapon.SkinID);
            pkt.WriteByte((byte)(weapon.OptionalSocketFlag == Item.OptionalSocket_All || weapon.OptionalSocketFlag == Item.OptionalSocket_PrimaryItem ? 1 : 0));

            // write mana stones
            pkt.WriteBytes(new byte[24]);

            pkt.WriteInt(0); // Godstone Item ID
            pkt.WriteInt(0);
            pkt.WriteInt(0);
            pkt.WriteInt(0);

            // item bonus format :
            // WriteShort(2560);
            // WriteShort(BonusType);
            // WriteInt(BonusValue);

            pkt.WriteShort(0);
            pkt.WriteUnsignedShort(weapon.Template.Mask);
            pkt.WriteLong(weapon.Count);
            pkt.WriteString(weapon.Crafter);
            pkt.WriteByte(0);
            pkt.WriteLong(weapon.Template.ExpireTime);
            pkt.WriteInt(weapon.TemporaryTradeTimeLeft);
            pkt.WriteInt(0);
            pkt.WriteShort(0);

            size = (int)pkt.Position - sizeLoc;
            placeHolder = (int)pkt.Position;
            pkt.Position = sizeLoc - 2;

            pkt.WriteUnsignedShort((ushort)size);
            pkt.Position = placeHolder;
        }

        public static void WriteArmor(AionServerPacket pkt, Item armor)
        {
            int placeHolder, sizeLoc, size = 0;
            short byteCount = 103;
            byteCount += (short)(armor.Crafter.Length * 2);
            pkt.WriteShort(byteCount);
            sizeLoc = (int)pkt.Position;

            pkt.WriteByte(6);
            pkt.WriteInt(armor.Equipped ? armor.Slot : 0);
            pkt.WriteByte(2);
            pkt.WriteInt((int)armor.Template.EquipmentSlot);
            pkt.WriteLong(0);
            pkt.WriteByte(0x0B);

            pkt.WriteByte((byte)(armor.SoulBound ? 1 : 0));
            pkt.WriteByte(armor.EnchantLevel);
            pkt.WriteInt(armor.SkinID == 0 ? armor.TemplateID : armor.SkinID);
            pkt.WriteByte((byte)(armor.OptionalSocketFlag == Item.OptionalSocket_All || armor.OptionalSocketFlag == Item.OptionalSocket_PrimaryItem ? 1 : 0));

            pkt.WriteBytes(new byte[24]);

            pkt.WriteInt(0);
            pkt.WriteByte(0);
            pkt.WriteInt(armor.Color);
            pkt.WriteLong(0);
            pkt.WriteByte(0);

            pkt.WriteUnsignedShort(armor.Template.Mask);
            pkt.WriteLong(armor.Count);
            pkt.WriteString(armor.Crafter);
            pkt.WriteByte(0);
            pkt.WriteLong(armor.Template.ExpireTime);
            pkt.WriteInt(armor.TemporaryTradeTimeLeft);
            pkt.WriteInt(0);
            pkt.WriteShort(0);

            size = (int)pkt.Position - sizeLoc;
            placeHolder = (int)pkt.Position;
            pkt.Position = sizeLoc - 2;

            pkt.WriteUnsignedShort((ushort)size);
            pkt.Position = placeHolder;

        }

        public static void WriteWeaponSwitch(AionServerPacket pkt, Item item)
        {
            pkt.WriteShort(5);
            pkt.WriteShort(6);
            pkt.WriteInt(item.Slot);
        }

        public static void WriteItem(AionServerPacket pkt, Item item)
        {
            ItemPacket.WriteGlobalInfo(pkt, item);
            if (item.TemplateID == 182400001)
            {
                ItemPacket.WriteKinah(pkt, item);
            }
            else if (item.Template.WeaponType != WeaponType.NoWeapon || item.Template.Name.Contains("arrow"))
            {
                ItemPacket.WriteWeapon(pkt, item);
                pkt.WriteShort((short)(item.Equipped ? 0xFF : item.Slot));
            }
            else if (item.Template.ArmorType != ArmorType.NoArmor)
            {
                ItemPacket.WriteArmor(pkt, item);
                pkt.WriteShort((short)(item.Equipped ? 0xFF : item.Slot));
            }
            else if (item.Template.StigmaGainedSkills.Count > 0)
            {
                ItemPacket.WriteStigmaStone(pkt, item);
            }
            else
            {
                ItemPacket.WriteCommonItem(pkt, item);
                pkt.WriteShort((short)(item.Equipped ? 0xFF : item.Slot));
            }
        }

    }

    public class SPkt_InventoryContents : AionServerPacket
    {
        IEnumerable<Item> storage;
        byte cubeLevel;

        public SPkt_InventoryContents()
        {
            storage = null;
        }

        public SPkt_InventoryContents(IEnumerable<Item> storage, byte cubeLevel)
        {
            this.cubeLevel = cubeLevel;
            this.storage = storage;
        }

        public override void WritePacket()
        {
            if (storage == null)
            {
                WriteInt(0);
                WriteShort(0);
                return;
            }

            WriteByte(1);
            WriteByte(cubeLevel);
            WriteShort(0);

            WriteShort((short)storage.Count());

            foreach (Item item in storage)
            {
                ItemPacket.WriteItem(this, item);
                WriteByte(0);
            }
        }
    }

    public class SPkt_InventorySizeUpdate : AionServerPacket
    {
        byte cubeType;
        byte advancedStigmaSlotsSize;

        public SPkt_InventorySizeUpdate(byte type, byte adv_stigmas)
        {
            cubeType = type;
            advancedStigmaSlotsSize = adv_stigmas;
        }

        public override void WritePacket()
        {
            WriteByte(cubeType);
            WriteByte(advancedStigmaSlotsSize);
            if (cubeType == 0)
            {
                WorldConnection wc = (WorldConnection)Connection;
                WriteInt(wc.Character.Inventory.Count);
                WriteByte((byte)((wc.Character.Inventory.Size - 27) / 3));
                WriteShort(0);
            }
        }
    }

    public class SPkt_InventoryContentsUpdate : AionServerPacket
    {
        IEnumerable<Item> items;
        StorageDisplayType displayType;
        public SPkt_InventoryContentsUpdate(IEnumerable<Item> items, StorageDisplayType displayType)
        {
            this.items = items;
            this.displayType = displayType;
        }
        public override void WritePacket()
        {
            WriteShort((short)(int)displayType);
            WriteShort((short)items.Count());
            foreach (Item item in items)
            {
                ItemPacket.WriteItem(this, item);
            }
        }
    }

    public class SPkt_WarehouseContentsUpdate : AionServerPacket
    {
        Item item;
        StorageType storageType;
        public SPkt_WarehouseContentsUpdate(Item item, StorageType type)
        {
            this.item = item;
            this.storageType = type;
        }
        public override void WritePacket()
        {
            WriteByte((byte)(int)storageType);
            WriteShort(19);
            WriteShort(1);

            ItemPacket.WriteItem(this, item);
        }
    }

    public class SPkt_InventoryItemDelete : AionServerPacket
    {
        Item item;
        public SPkt_InventoryItemDelete(Item item)
        {
            this.item = item;
        }
        public override void WritePacket()
        {
            WriteInt(item.GetGUID());
            WriteByte(0);
        }
    }

    public class SPkt_WarehouseItemDelete : AionServerPacket
    {
        Item item;
        StorageType type;
        public SPkt_WarehouseItemDelete(Item item, StorageType type)
        {
            this.item = item;
            this.type = type;
        }
        public override void WritePacket()
        {
            WriteByte((byte)(int)type);
            WriteInt(item.GetGUID());
            WriteByte(14);
        }
    }

    public class SPkt_InventoryItemUpdate : AionServerPacket
    {
        bool weaponSwitch;
        Item item;
        public SPkt_InventoryItemUpdate(Item item, bool weaponSwitch)
        {
            this.weaponSwitch = weaponSwitch;
            this.item = item;
        }
        public override void WritePacket()
        {
            ItemPacket.WriteGlobalInfo(this, item);
            if (item.TemplateID == 182400001)
            {
                ItemPacket.WriteKinah(this, item);
                WriteByte(0);
                WriteShort(0x1a);
            }
            else if (item.Template.WeaponType != WeaponType.NoWeapon || item.Template.Name.Contains("arrow"))
            {
                if (weaponSwitch)
                    ItemPacket.WriteWeaponSwitch(this, item);
                else
                {
                    ItemPacket.WriteWeapon(this, item);
                    WriteShort((short)(item.Equipped ? 0xFF : item.Slot));
                    WriteByte(0);
                }
            }
            else if (item.Template.ArmorType != ArmorType.NoArmor)
            {
                ItemPacket.WriteArmor(this, item);
                WriteShort((short)(item.Equipped ? 0xFF : item.Slot));
                WriteByte(0);
            }
            else if (item.Template.StigmaGainedSkills.Count > 0)
            {
                ItemPacket.WriteStigmaStone(this, item);
            }
            else
            {
                ItemPacket.WriteCommonItem(this, item);
                WriteShort((short)(item.Equipped ? 0xFF : item.Slot));
                WriteByte(0);
            }
        }
    }

    public class SPkt_WarehouseItemUpdate : AionServerPacket
    {
        Item item;
        StorageType type;
        public SPkt_WarehouseItemUpdate(Item item, StorageType type)
        {
            this.item = item;
            this.type = type;
        }
        public override void WritePacket()
        {
            WriteInt(item.GetGUID());
            WriteByte((byte)(int)type);
            WriteShort(0x24);
            WriteInt(item.Template.NameID);
            WriteShort(0);

            if (item.TemplateID == 182400001)
            {
                ItemPacket.WriteKinah(this, item);
            }
            else if (item.Template.WeaponType != WeaponType.NoWeapon || item.Template.Name.Contains("arrow"))
            {
                ItemPacket.WriteWeapon(this, item);
                WriteShort((short)(item.Equipped ? 0xFF : item.Slot));
            }
            else if (item.Template.ArmorType != ArmorType.NoArmor)
            {
                ItemPacket.WriteArmor(this, item);
                WriteShort((short)(item.Equipped ? 0xFF : item.Slot));
            }
            else if (item.Template.StigmaGainedSkills.Count > 0)
            {
                ItemPacket.WriteStigmaStone(this, item);
            }
            else
            {
                ItemPacket.WriteCommonItem(this, item);
                WriteShort((short)(item.Equipped ? 0xFF : item.Slot));
            }
        }
    }

}
